﻿var delete_device = function (id) {
    var aaa = confirm("确认删除吗？");
    if (aaa == true) {

        
        var deviceid = id;
        $.ajax({
            url: "Ajax/QueryDevice.aspx?typedevice=delete",
            type: "post",
            dataType: "json",
            data: { device_id: deviceid },
            success: function (msg) {
                alert(msg);
                $("tr#" + id + "_deviceinfo").remove();
            }
        })
    }
}
var type_sum;
function load_type2() {
    var obj = document.getElementById("device_type2");
   
    document.getElementById("device_type2").options.length = 0;
    $.ajax({
        url: "Ajax/QueryDevice.aspx?typedevice=gettype",
        type: "post",
        dataType: "json",
        data: {},
        success: function (msg) {
            $(msg).each(function () {
                var option;
                var txt = this.name
                var val = this.id;
                option = new Option(txt, val);
                obj.options.add(option);
            })
           
            var option;
            var txt = "新的设备类型";
            var val = -1;
            option = new Option(txt, val);
            obj.options.add(option);
        }
    })
    

}
function load_device() {
    $.ajax({
        url: "Ajax/QueryDevice.aspx?typedevice=state",
        type: "post",
        dataType: "json",
        data: {},
        success: function (msg) {
            $("table#rd_table tr:gt(0)").remove();
            $(msg).each(function () {

                var id = this.id;
                var device_name = this.device_name;
                var type_name = this.type_name;
                var used = this.used;
                var state;
                if (used == true) {
                    state = "已借出";
                }
                else {
                    state = "可用";
                }
                var begin_time = this.from;
                var end_time = this.to;
                var borrower = this.user_name;
                var button = "<a href='javascript:;' onclick='delete_device(id)' class='delete_device' id=" + id + ">删除</a>";
                var row = "<tr class='record_row' id='" + id + "_deviceinfo'><td align='center'>" + this.id + "</td><td align='center'>" + this.device_name + "</td><td align=center>" + this.type_name + "</td><td align=center>" + state + "</td><td align=center>" + begin_time + "</td><td align=center>" + end_time + "</td><td align=center>" + borrower + "</td><td align=center>" + button + "</td></tr>";
                var row_num = $("table#rd_table tr").length - 1;
                $(row).insertAfter($("table#rd_table tr:eq(" + row_num + ")"));
            })
        }
    })
}

var type_selchange = function () {
    var type_id = $("#device_type2").val();
    if (type_id == -1) {
        $("#newtype_name").show();
        $("#newdevice_type").show();
    }
    else {
        $("#newtype_name").hide();
        $("#newdevice_type").hide();
    }
}

$("document").ready(function () {
    
    $("#newdevice_type").hide();
    $("#newtype_name").hide();
    load_type2();
    load_device();
    var type_name = null;
    $("#add_device").mouseup(function () {
        var newdevice_name = $("#newdevice_name").val();
        var device_type = $("#device_type2").val();

        type_name = $("#newdevice_type").val();
        if (device_type != -1) {
            type_name = "ok";
        }
        if (newdevice_name == "") {
            alert("请输入新设备的名称");
        }

        else if (type_name == "") {
            alert("请输入新设备的类型");
        }

        else {
            $.ajax({
                url: "Ajax/QueryDevice.aspx?typedevice=adddevice",
                type: "post",
                dataType: "json",
                data: { device_name: newdevice_name, type_id: device_type, name: type_name },
                success: function (msg) {
                    alert(msg);
                    load_type2();
                    load_device();

                }
            })
        }

    })
})